const exprima = require("esprima");
const extraverse = require("estraverse");
const escodegen = require("escodegen");

const code = `function a(){}`;

const ast = exprima.parseScript(code);
console.log("ast:", ast);

extraverse.traverse(ast, {
  enter(node) {
    console.log("node enter", node.type);

    // 更改源码
    if (node.type === "FunctionDeclaration") {
      // 修改函数名， 把 code 字符串的函数中，函数名 a 修改成 astFn
      node.id.name = "astFn";
    }
  },
  leave(node) {
    console.log("node leave", node.type);
  },
});

const generateCode = escodegen.generate(ast);

console.log("generateCode: ", generateCode);
